<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appName">
              <a-select v-model="model.appName" placeholder="请选择项目名称" @change="getAppId">
                <a-select-option v-for="item in appList" :key="item.appId" :value="item.label">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="APPID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appId">
              <a-input v-model="model.appId" readOnly placeholder="请输入APPID"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="应用版本号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imei">
              <a-input v-model="model.imei" placeholder="请输入应用版本号"  ></a-input>
              <p style="margin:0;line-height: 20px;color: #f5222d;">对应uni-app 中的应用版本号，例：233</p>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="应用版本名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="version">
              <a-input v-model="model.version" placeholder="请输入应用版本名称"  ></a-input>
              <p style="margin:0;line-height: 20px;color: #f5222d;">对应uni-app 中的应用版本名称，例：1.0.0</p>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="Apk文件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload class="avatar-uploader"   text="上传" v-model="model.url"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="wgt文件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload class="avatar-uploader"   text="上传" v-model="model.wgtUrl"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="更新版本内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="note">
              <a-textarea v-model="model.note" rows="4" placeholder="请输入更新版本内容" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'SkAppVersionForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           appId: [
              { required: true, message: '请选择APPID!'},
           ],
           appName: [
              { required: true, message: '请选择项目名称!'},
           ],
           version: [
              { required: true, message: '请输入应用版本名称!'},
           ],
           imei: [
              { required: true, message: '请输入应用版本号!'},
           ],
           note: [
              { required: true, message: '请输入版本介绍!'},
           ],
        },
        url: {
          add: "/management/skAppVersion/add",
          edit: "/management/skAppVersion/edit",
          queryById: "/management/skAppVersion/queryById"
        },
        appList:[
          {label:"峰云教务",appId:"__UNI__BC586F1"},
          {label:"峰云班牌",appId:"__UNI__8DC10D2"}
        ]
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      getAppId(data){
        this.model.appId = this.appList.find(item=>item.label == data).appId;
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>